package com.wangfugui.mybatisplus.tool;

import com.wangfugui.mybatisplus.common.utils.WebpUtils;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;


public class WebPConverter {

    private static final Set<String> SUPPORTED_EXTENSIONS = new HashSet<>(
            Arrays.asList("jpg", "jpeg", "png"));
    private static final String WEBP_EXTENSION = "webp";

    /**
     * 递归转换目录中的图片为WebP格式
     *
     * @param directoryPath 目录路径
     * @param quality       转换质量 (0.0-1.0)
     * @return 转换成功的文件数量
     */
    public static int convertDirectoryToWebP(String directoryPath, float quality) throws IOException {
        File directory = new File(directoryPath);
        if (!directory.exists() || !directory.isDirectory()) {
            throw new IllegalArgumentException("路径不存在或不是目录: " + directoryPath);
        }

        return processDirectory(directory, quality);
    }

    private static int processDirectory(File directory, float quality) throws IOException {
        int convertedCount = 0;
        File[] files = directory.listFiles();
        if (files == null) return 0;

        for (File file : files) {
            if (file.isDirectory()) {
                convertedCount += processDirectory(file, quality);
            } else if (isSupportedImage(file)) {
                if (convertToWebP(file, quality)) {
                    convertedCount++;
                }
            }
        }
        return convertedCount;
    }

    private static boolean isSupportedImage(File file) {
        String fileName = file.getName().toLowerCase();
        int dotIndex = fileName.lastIndexOf('.');
        if (dotIndex == -1) return false;

        String extension = fileName.substring(dotIndex + 1);
        return SUPPORTED_EXTENSIONS.contains(extension);
    }
    private static final String CWEBP_LINUX = "cwebp_win64";
    private static boolean convertToWebP(File originalFile, float quality) {
        String originalPath = originalFile.getAbsolutePath();
        String webpPath = originalPath.substring(0, originalPath.lastIndexOf('.')) + "." + WEBP_EXTENSION;

        boolean b  = WebpUtils.convertToWebp(originalPath, webpPath, 80, "F:\\工作\\workspace\\liyu\\spring-boot-mybatisplus\\mybatisplus\\src\\main\\resources\\libwebp\\" + CWEBP_LINUX);
        if(b){
            System.out.println("转换成功: " + originalPath + " -> " + webpPath);
        }else{
            System.out.println("转换失败: " + originalPath + " -> " + webpPath);
        }
        return b;
    }

    public static void main(String[] args) {
        try {
            String directoryPath = "F:\\短剧\\01短剧"; // 替换为你的测试目录
            float quality = 0.8f;

            System.out.println("开始转换目录: " + directoryPath);
            int convertedCount = convertDirectoryToWebP(directoryPath, quality);
            System.out.println("转换完成，共转换 " + convertedCount + " 个文件");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}